package com.fw.application.controller;


import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.fw.application.controller.base.BaseController;
import com.fw.common.Builder;
import com.fw.common.IdXD;
import com.fw.mes.Result;
import com.fw.system.web.model.entity.TbAmountLog;
import com.fw.system.web.model.entity.TbMoneys;
import com.fw.system.web.model.entity.TbRuleDates;
import com.fw.system.web.model.entity.TbUser;
import com.fw.system.web.service.ITbAboutService;
import com.fw.system.web.service.ITbAmountLogService;
import com.fw.system.web.service.ITbMoneysService;
import com.fw.system.web.service.ITbRuleDatesService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.mysql.cj.protocol.ResultsetRow;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.jsoup.Connection;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

import static com.fw.mes.ResultUtils.success;


@RestController
@RequestMapping("/amount")
@Api(tags = "收益支出和充值提现相关")
@CrossOrigin
public class TbAmountLogController implements BaseController {

    @Autowired
    private ITbAmountLogService amountLogService;
    @Autowired
    private ITbAboutService aboutService;
    @Autowired
    private IdXD idXD;
    @Autowired
    private ITbMoneysService moneysService;
    @Autowired
    private ITbRuleDatesService ruleDatesService;



    @GetMapping("/getLogList")
    @ApiOperation("查看充值 提现 消费 收益明细 type 1 2 3 4")
    public Result getLogList(@RequestParam Integer type, @RequestParam Integer pageNum, @RequestParam Integer pageSize) {

        String userId = this.getUser().getId();
        PageHelper.startPage(pageNum, pageSize);
        List<TbAmountLog> list= new ArrayList<>();
        if (type.equals(2)) {
            list = amountLogService.list(Wrappers.<TbAmountLog>lambdaQuery().eq(TbAmountLog::getUserId, userId)
                    .eq(TbAmountLog::getLogType, type).orderByDesc(TbAmountLog::getCreateTime));
        }else {
            list = amountLogService.list(Wrappers.<TbAmountLog>lambdaQuery().eq(TbAmountLog::getUserId, userId)
                    .eq(TbAmountLog::getLogType, "1").or(true).eq(TbAmountLog::getUserId, userId).eq(TbAmountLog::getLogType,"3").
                            or(true).eq(TbAmountLog::getUserId, userId).eq(TbAmountLog::getLogType,"4").orderByDesc(TbAmountLog::getCreateTime));
        }
        PageInfo<Object> pageInfo = new PageInfo(list);
        return success(pageInfo);
    }

    @PostMapping("/payAmount")
    @ApiOperation("充值 logType 1充值 logCount 金额 imgUrl 凭证地址")
    public Result payAmount(@RequestBody TbAmountLog amountLog) {
        TbAmountLog one = amountLogService.getOne(Wrappers.<TbAmountLog>lambdaQuery().eq(TbAmountLog::getUserId, this.getUser().getId()).eq(TbAmountLog::getIsStatus, 1)
                .eq(TbAmountLog::getLogType, 1));
        if (one != null)
            return new Result().fail(0, "您有一个待审核的充值！");
        amountLog.setId(idXD.nextId());
        amountLog.setCreateTime(LocalDateTime.now());
        amountLog.setIsStatus(1);
        amountLog.setLogType(1);
        amountLog.setUserId(this.getUser().getId());
        amountLog.insert();
        return success();
    }

    @GetMapping("/getPayCode")
    @ApiOperation("获得平台收款码")
    public Result getPayCode() {
        return success(aboutService.getById("1"));
    }

    @PostMapping("/withdrawal")
    @ApiOperation("提现申请 logType 2提现 logCount 金额 isStatus 1提现到支付宝 2提现到银行卡")
    public Result withdrawal(@RequestBody TbAmountLog amountLog) {
        TbUser user = this.getUser();
        TbRuleDates ruleDates = ruleDatesService.getById("3");
        TbRuleDates ruleDates1 = ruleDatesService.getById("4");
        if (amountLog.getLogCount().compareTo(ruleDates.getRuleCount())<0)
            return new Result().fail(0,"最低输入金额"+ruleDates.getRuleCount());
        if (amountLog.getLogCount().compareTo(ruleDates1.getRuleCount())>0)
            return new Result().fail(0,"最高输入金额"+ruleDates1.getRuleCount());
        TbMoneys moneys = moneysService.getOne(Wrappers.<TbMoneys>lambdaQuery().eq(TbMoneys::getUserId, this.getUser().getId()));
        if (moneys.getIsStatus().equals(0))
            return new Result().fail(0, "对不起您的账户被封禁");

        if(amountLog.getIsStatus().equals(1)){
            if (StringUtils.isBlank(user.getAliAccount()))
                return new Result().fail(0,"请去绑定支付宝账户");
            amountLog.setLogName("提现到支付宝");
        }else{
            if (StringUtils.isBlank(user.getBankCar()))
                return new Result().fail(0,"请去绑定银行卡账户");
            amountLog.setLogName("提现到银行卡");
        }
        TbRuleDates dates = ruleDatesService.getById("5");
        BigDecimal divide = amountLog.getLogCount().multiply(dates.getRuleCount()).divide(new BigDecimal("100"), 3, BigDecimal.ROUND_FLOOR);
        BigDecimal add = amountLog.getLogCount().subtract(divide);
        if (moneys.getAmount().compareTo(amountLog.getLogCount()) < 0)
            return new Result().fail(0, "余额不足");
        moneys.setAmount(moneys.getAmount().subtract(amountLog.getLogCount()));
        moneysService.updateById(moneys);
        amountLog.setId(idXD.nextId());
        amountLog.setCreateTime(LocalDateTime.now());
        amountLog.setIsStatus(1);
        amountLog.setLogType(2);
        amountLog.setUserId(this.getUser().getId());
        amountLog.insert();
        Builder.of(TbAmountLog::new).with(TbAmountLog::setId,idXD.nextId())
                .with(TbAmountLog::setCreateTime,LocalDateTime.now())
                .with(TbAmountLog::setLogType,3)
                .with(TbAmountLog::setUserId,user.getId())
                .with(TbAmountLog::setLogName,"提现"+amountLog.getLogCount()+"服务费"+divide+"实际应到账"+add).build().insert();
        return success();
    }

    @GetMapping("/getMoney")
    @ApiOperation("查寻体现手续费")
    public Result getMoney(){

        BigDecimal ruleCount = ruleDatesService.getById("5").getRuleCount();

        return success(ruleCount);
    }

}

